package com.reggie.serve.common;

import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.util.HashMap;
import java.util.Map;

@Data
@NoArgsConstructor
public class R<T> {

    private static final int SUCCESS_CODE = 1;
    private static final int ERROR_CODE = 0;

    private Integer code;
    private String msg;
    private T data;
    private Map<String, Object> map = new HashMap<>();

    @Builder
    public R(Integer code, String msg, T data) {
        this.code = code;
        this.msg = msg;
        this.data = data;
    }

    public static <T> R<T> success(T object) {
        return R.<T>builder().code(SUCCESS_CODE).data(object).build();
    }

    public static <T> R<T> error(String msg) {
        return R.<T>builder().code(ERROR_CODE).msg(msg).build();
    }

    public R<T> add(String key, Object value) {
        if (key != null) {
            this.map.put(key, value);
        }
        return this;
    }
}
